<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Pub extends Stourweb_Controller
{
    public $supplier_id;

    /**
     * 初始化支付对象
     */
    public function before()
    {
        parent::before();
        //登录状态判断
        $this->supplier_id = Cookie::get('st_supplier_id');
    }

    /**
     * 公共头部
     */
    public function action_header()
    {
        $supplier_id = Cookie::get('st_supplier_id');
        $user_info = Model_Supplier::get_supplier_byid($supplier_id);

        $this->assign('userinfo',$user_info);
        $this->display("pub/header");
    }

    /**
     * 左侧菜单栏
     */
    public function action_left_menu()
    {
        /*查询当前供应商是否有扩展应用*/
        $supplier = DB::select()->from('supplier')->where("id={$this->supplier_id}")->execute()->current();
        $authorization = $supplier['authorization'];
        $sql = '';

        if($authorization && !in_array($supplier['verifystatus'],array(0,1,2))){
            $model_list = DB::select()->from('model')->where("id IN({$authorization})")->execute()->as_array();
        }else{
            $sql = 'and id NOT IN(2,3,4,5,6)';
        }

        $menu = DB::select()->from('supplier_menu')->where("pid=0 and isshow=1 {$sql}")->order_by('displayorder','ASC')->execute()->as_array();
        foreach ($menu as &$top){
            $top['url'] = self::menu_url($top);
            if(!empty($sql) && $top['id']==1){
                $top['url'] = '/supplier/pc/qualify/step';
            }
            if(isset($model_list) && in_array($top['id'],array(2,4))){
                $arr = array();
                foreach ($model_list as $k=>$m){
                    $sub_menu = DB::select()->from('supplier_menu')
                        ->where("pid={$top['id']} and isshow=1 and type_id={$m['id']}")
                        ->execute()->current();
                    if(!$sub_menu){
                        continue;
                    }
                    $arr[$k]['level']        = 1;
                    $arr[$k]['title']        = $m['modulename'];
                    $arr[$k]['type_id']      = $m['id'];
                    if($m['id']>199){
                        $arr[$k]['data_info'] = json_encode(array('type_id'=>$m['id']));
                    }else{
                        $arr[$k]['data_info'] = $sub_menu['data_info']?$sub_menu['data_info']:'';
                    }
                    $arr[$k]['isshow']       = 1;
                    $arr[$k]['displayorder'] = $m['id'];
                    if($sub_menu){
                        $arr[$k]['directory']    = $sub_menu['directory'];
                        $arr[$k]['controller']   = $sub_menu['controller'];
                        $arr[$k]['method']       = $sub_menu['method'];
                    }
                    if($top['id']==2){
                        if(!$sub_menu){
                            $arr[$k]['directory']    = '/plugins/supplier_tongyong';
                            $arr[$k]['controller']   = 'index';
                            $arr[$k]['method']       = 'list';
                        }
                        $arr[$k]['id']           = $m['id'].'_2';
                        $arr[$k]['pid']          = $top['id'];
                    }elseif($top['id']==4){
                        if(!$sub_menu){
                            $arr[$k]['directory']    = '/plugins/supplier_tongyong';
                            $arr[$k]['controller']   = 'order';
                            $arr[$k]['method']       = 'all';
                        }
                        $arr[$k]['id']           = $m['id'].'_4';
                        $arr[$k]['pid']          = $top['id'];
                    }
                }
                $top['sub_menu'] = $arr;
            }else{
                $top['sub_menu'] = DB::select()->from('supplier_menu')
                    ->where("pid={$top['id']} and isshow=1")
                    ->order_by('displayorder','ASC')
                    ->execute()->as_array();
            }
            foreach ($top['sub_menu'] as &$sub){
                $sub['url'] = self::menu_url($sub);
                if($sub['data_info']){
                    $data_info = json_decode($sub['data_info'],true);
                    $sub['url'].= '?';
                    foreach ($data_info as $k=>$v){
                        $sub['url'].= $k.'='.$v;
                    }
                }
            }
        }

        $this->assign('menu',$menu);
        $this->display('pub/left_menu');
    }

    public function menu_url($data)
    {
        $url = '';
        if($data['directory']){
            $url .= $data['directory'].'/';
        }
        if($data['controller'] ){
            $url .= $data['controller'].'/';
        }
        if($data['method']){
            $url .= $data['method'].'/';
        }

        return $url;
    }

    /**
     * ajax 验证码验证
     */
    public function action_ajax_do_code()
    {
        $code = Common::remove_xss(Arr::get($_POST,'code'));

        if (!Captcha::valid($code))
        {
            echo json_encode(array('status' => 0));
            exit;
        }

        echo json_encode(array('status' => 1));
    }

    /**
     * ajax 发送验证码
     */
    public function action_ajax_send_message()
    {
        //检测用户是否存在
        $phone = Arr::get($_POST, 'phone');
        $email = Arr::get($_POST, 'email');
        $type = Arr::get($_POST, "type");
        $code = rand(1000, 9999);

        if (empty($phone))
        {
            //email send code
            $validataion = Validation::factory($this->request->post());
            $validataion->rule('email', 'not_empty');
            $validataion->rule('email', 'email');
            if (!$validataion->check())
            {
                exit(__('error_user_email'));
            }

            require_once TOOLS_COMMON . 'email/emailservice.php';
            Common::session('msg_code', null);

            $status = EmailService::send_member_email($email, ($type == 'findpass_code' ? NoticeCommon::MEMBER_FINDPWD_CODE_MSGTAG : NoticeCommon::MEMBER_REG_CODE_MSGTAG), "", $code);
            if ($status)
            {
                Common::session('msg_code', $code);
                Common::session('msg_account', $email);
            }
            echo intval($status);
        } else
        {
            //phone send code
            $validataion = Validation::factory($this->request->post());
            $validataion->rule('phone', 'not_empty');
            $validataion->rule('phone', 'phone');
            if (!$validataion->check())
            {
                exit(__('error_user_phone'));
            }

            require_once TOOLS_COMMON . 'sms/smsservice.php';
            Common::session('msg_code', null);

            $status = SMSService::send_member_msg($phone, ($type == 'findpass_code' ? NoticeCommon::MEMBER_FINDPWD_CODE_MSGTAG : NoticeCommon::MEMBER_REG_CODE_MSGTAG), "", "", $code);
            $status = json_decode($status);
            if ($status->Success)
            {
                Common::session('msg_code', $code);
                Common::session('msg_account', $phone);
            }
            echo intval($status->Success);
        }

    }

}